package org.yylnb.openplatform.commons.utlis;

import org.yylnb.openplatform.commons.constans.ErrorCode;

import java.io.Serializable;

/**
 * @author RainLin
 * @date 2020/7/17 - 19:36
 */
public class CommonsResult<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    /**
     * 默认操作成功
     */
    public static final CommonsResult SUCCESS = new CommonsResult(ErrorCode.SUCCESS, "成功");
    /**
     * 默认操作失败
     */
    public static final CommonsResult ERROR = new CommonsResult(ErrorCode.ERROR, "失败");

    /**
     * 用户名或密码错误
     */
    public static final CommonsResult PARAMS_ERROR = new CommonsResult(ErrorCode.PARAMS_ERROR, "参数错误");

    /**
     * 找不到资源错误
     */
    public static final CommonsResult NOT_FOUND_ERROR = new CommonsResult(ErrorCode.NOT_FOUND_ERROR, "找不到该资源");

    /**
     * 服务器异常
     */
    public static final CommonsResult SERVER_ERROR = new CommonsResult(ErrorCode.SERVER_ERROR, "服务器发生了异常");


    public CommonsResult() {
    }

    private CommonsResult(Integer code, String msg) {
        this(code, msg, null);
    }

    private CommonsResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private CommonsResult(CommonsResult<T> result) {
        this.code = result.getCode();
        this.msg = result.getMsg();
        this.data = null;
    }


    public CommonsResult<T> code(Integer code) {
        CommonsResult<T> result = new CommonsResult<T>(this);
        result.setCode(code);
        return result;
    }

    public CommonsResult<T> msg(String msg) {
        CommonsResult<T> result = new CommonsResult<T>(this);
        result.setMsg(msg);
        return result;
    }

    public CommonsResult<T> data(T data) {
        CommonsResult<T> result = new CommonsResult<T>(this);
        result.setData(data);
        return result;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
